package com.lp.mgt.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lp.mgt.commons.CustomResult;
import com.lp.mgt.entities.Post;
import com.lp.mgt.services.PostService;

@RestController
@RequestMapping("/post")
public class PostCtr {
	@Autowired
	private PostService postService;
	
	
	@RequestMapping("/listPage")
	public ModelAndView toPostPage(ModelAndView mv) {
		mv.setViewName("post/postList.html");
		return mv;
	}
	
	
	
	@RequestMapping("/getPostsByDeptId")
	public CustomResult<Post> getPostsByDept(String deptId){
		List<Post> posts=postService.getPostsByDeptId(deptId);
		return new CustomResult().success(true, posts);
	}
	
	@RequestMapping("/list")
	public CustomResult<Post> listPostsWithTerms(Post post){
		Map<String,Object> map=new HashMap<>();
		map=postService.listPostsWithTerms(post);
		return new CustomResult().success(true, map);
	}
	
	@PostMapping("/savePost")
	public CustomResult<Post> savePost(Post post){
		postService.savePost(post);
		return new CustomResult().success(true);
	}
	
	@PostMapping("/editPost")
	public CustomResult<Post> editPost(Post post){
		postService.editPost(post);
		return new CustomResult().success(true);
	}
	
	@GetMapping("/removePost")
	public CustomResult<Post> removePost(String postId){
		postService.removePostByPostId(postId);
		return new CustomResult().success(true);
	}
}
